<?php

namespace app\index\model;

use think\Model;
use think\Validate;
use app\index\model\User;

/**
 * 基类模型
 * Class Base
 * @package app\admin\model
 */
class Base extends Model
{

    protected $prefix = 'shop_';//表前缀
    protected $after_limit;//分页大小
    protected $after_page;//分页大小
    protected $admin;//用户
    protected $substations;//当前分站列表
    protected $user;//用户基础信息
    protected $user_id;//用户ID
    protected $key;

    //初始化
    public function initialize(){

        //调用父类初始化
        parent::initialize();

        //获取分页信息
        $this->after_limit = session("after_limit");
        $this->after_page = session("after_page");

        //获取用户信息
        $this->admin = session("admin");

        //当前分站设置
        $this->substations = session("substations");
        //获取用户信息
        $this->user = session("user");
        $this->user_id = session("user_id");
        //用户cookie
        $this->key = md5(cookie('cookie_name'));
    }


    //检查数据是否符合规则
    public function checkData($data,$check){
        $is = true;
        $validate = new Validate($check);
        if (!$validate->check($data)) {
            $is = $validate->getError();
        }
        return $is;
    }

    /**
     * Author:Jack Wang
     * Time:2018-10-18 17:22:46
     * Describe:基类添加修改
     * @param $data
     * @param string $filed_id
     * @param int $id
     * @return bool|false|int
     */
    public function mySave($data,$filed_id = "id",$id = 0){
        $state = false;
        if(!empty($id) || !empty($data[$filed_id])){
            if(!empty($data[$filed_id])){
                //获取id，传入获取id参数为主没有传入直接获取data中的获取id
                $id = !empty($id) ? $id :$data[$filed_id];
                unset($data[$filed_id]);//修改时候移除menu_id
            }
            $state = $this->allowField(true)->isUpdate(true)->save($data,[$filed_id=>$id]);
        }else{
            $state = $this->allowField(true)->isUpdate(false)->data($data)->save();
        }
        return $state;
    }

    /**
     * Author:Jack Wang
     * Time:2018-10-19 11:53:50
     * Describe:基类多数据添加修改
     * @param $data
     * @param bool $isAdd
     * @return array|bool|false
     * @throws \Exception
     */
    public function mySaveAll($data,$isAdd = false){
        $state = false;
        if($isAdd){
            $state = $this->allowField(true)->saveAll($data,false);
        }else{
            $state = $this->allowField(true)->saveAll($data);
        }
        return $state;
    }

    /**
     * Author:Jack Wang
     * Time:2018-10-23 11:42:25
     * Describe:获取某个字段的值
     * @param $field_where
     * @param $where
     * @param $get_field
     * @return mixed
     */
    public function getFieldValue($field_where,$where,$get_field){
        return $this->where($field_where,$where)->value($get_field);
    }

    /**
     * Author:Jack Wang
     * Time:2018-10-23 14:56:09
     * Describe:更新某个字段的值
     * @param $field_where
     * @param $where
     * @param $set_field
     * @param $value
     * @return int
     */
    public function setFieldValue($field_where,$where,$set_field,$value){
        return $this->where($field_where,$where)->setField($set_field,$value);
    }

    /**
     * Author:Jack Wang
     * Time:2018-10-24 14:43:17
     * Describe:检查字段值是否唯一
     * @param $field_where
     * @param $where
     * @param $id_field
     * @param $id
     * @return int|string
     */
    public function checkFieldOnly($field_where,$where,$id_field,$id){
        return $this->where($field_where,$where)->where($id_field,"neq",$id)->where(config("TableDeleteField"),0)->count();
    }

    /**
     * Author:Jack Wang
     * Time:2018-11-22 10:51:03
     * Describe:获取详细信息
     * @param $field_where
     * @param $where
     * @return array|false|\PDOStatement|string|Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function dataInfo($field_where,$where){
        return $this->where($field_where,$where)->find();
    }

    //删除
    public function del($field_where,$where,$is = false){
        if(!$is) {
            return $this->where($field_where, $where)->setField(config("TableDeleteField"), 1);
        }else{
            return $this->where($field_where, $where)->delete();
        }
    }

    //获取条件个数
    public function getFieldCount($field_where,$where){
        return $this->where($field_where,$where)->count();
    }

    /**
     * Author:you
     * Time:2018-07-13 16:01:39
     * Describe:根据ID获取信息
     * @param $id
     * @return array|false|\PDOStatement|string|\think\Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function info($id){
        return $this->alias("a")->field("a.*")
            ->find($id);
    }

    /**
     * Author:Jack Wang
     * Time:2018-11-01 11:33:27
     * Describe:强制新增
     * @param $data
     * @return false|int
     */
    public function add($data = []){
        return $this->allowField(true)->data($data)->isUpdate(false)->save();
    }

}